﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Vit.Database.DataAccess;
using Vit.Database.Schema;

namespace Vit.Database
{
    public abstract class DatabaseWrapper
    {
        protected DatabaseBase db;
        protected DatabaseSchema dbSchema;
        protected DatabaseInfo dbInfo;
        protected DatabaseFileInfo dbFile;
        protected DatabaseBackupInfo dbBackup;
        protected string connectionString;
        protected IDataInstrumentationProvider dataInstrumentationProvider;

        public DatabaseWrapper(string connectionString)
            : this(connectionString, new NullDataInstrumentationProvider())
        {
        }

        public DatabaseWrapper(string connectionString, IDataInstrumentationProvider dataInstrumentationProvider)
        {
            if (dataInstrumentationProvider == null)
                dataInstrumentationProvider = new NullDataInstrumentationProvider();
            this.connectionString = connectionString;
            this.dataInstrumentationProvider = dataInstrumentationProvider;
        }

        public virtual void Reload()
        {
            InitialDatabase();
            RefreshDatabaseSchemas();
            RefreshDatabaseInfo();
        }

        public virtual void CreateUtilities()
        {
            CreateKillThreadsUtility();
            CreateViewBlockThreadsUtility();
            CreateSchemaInfoUtility();
            CreateDatabaseInfoUtility();
        }

        public abstract void InitialDatabase();
        public abstract void RefreshDatabaseSchemas();
        public abstract void RefreshDatabaseInfo();

        public abstract void KillThreads();
        public abstract void ViewBlockThreads();

        public abstract void CreateKillThreadsUtility();
        public abstract void CreateViewBlockThreadsUtility();
        public abstract void CreateSchemaInfoUtility();
        public abstract void CreateDatabaseInfoUtility();

        public abstract DbType ToDbType(string nativeType);
        public abstract Type ToSystemType(string nativeType, bool nullable);

        public abstract object ConvertValueType(string nativeType, object value);

        public abstract string BuildFullName(string shcema, string owner, string name);
        public abstract string BuildSafeName(string name);
        public abstract string BuildParameterName(string name);
        public abstract string BuildOriginalParameterName(string name);

        public DatabaseBase Database { get { return db; } }
        public DatabaseSchema DatabaseSchema { get { return dbSchema; } }
        public DatabaseInfo DatabaseInfo { get { return dbInfo; } }
        public DatabaseFileInfo DatabaseFileInfo { get { return dbFile; } }
        public DatabaseBackupInfo DatabaseBackupInfo { get { return dbBackup; } }
    }
}
